package cn.wolfcode.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;


    @RequiredPermission(name="试卷页面",expression="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "test/新增试卷页面";
    }


    @RequiredPermission(name="试卷删除",expression="exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           examService.delete(id);
        }
        return "redirect:/exam/list";
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Exam exam) {
        if (exam.getId() == null) {
            examService.save(exam);
        } else {
            examService.update(exam);
        }
        return "redirect:/exam/list";
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("exam",examService.get(id));
        }
        return "exam/input";
    }
}
